package com.ramos.kuaixiao.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.ramos.kuaixiao.entity.ClientPrice;

import java.util.List;

//商店与价格套本地数据
public class ClientPriceDB {
    private static String TAG = "ClientPriceDB";
    private static String TABLE_NAME = "ClientPrice";
    public static final String CREATE_TABLE_SQL;
    public static final String DROP_USERINFO;

    static {
        CREATE_TABLE_SQL = "CREATE TABLE " + TABLE_NAME + " (" + "id" + " CHAR(30) PRIMARY KEY NOT NULL,"
                + "dealerId" + " CHAR(30)," + "priceTypeId" + " CHAR(30)," + "orgId" + " CHAR(30)," + "salerId" + "  CHAR(30),"+ "customerId" + "  CHAR(30),"+ "modifyTime" + " Long);";
        DROP_USERINFO = "DROP TABLE IF EXISTS " + TABLE_NAME;
    }

    public int addAllClientPrice(List<ClientPrice> customer) {
        //deleteAll();
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM ClientPrice");
        String sql = "insert into ClientPrice(id,dealerId,priceTypeId,orgId,salerId,customerId,modifyTime) values(?,?,?,?,?,?)";
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        SQLiteStatement stmt = db.compileStatement(sql);
        for (int i = 0; i < customer.size(); i++) {
            stmt.bindString(1, customer.get(i).getDealerId() + "");
            stmt.bindString(2, customer.get(i).getPriceTypeId() + "");
            stmt.bindString(3, customer.get(i).getOrgId() + "");
            stmt.bindString(4, customer.get(i).getSalerId() + "");
            stmt.bindString(5, customer.get(i).getCustomerId() + "");
            stmt.bindString(6, customer.get(i).getModifyTime() + "");
            stmt.execute();
            stmt.clearBindings();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public int updateAllClientPrice(List<ClientPrice> customer) {
        SQLiteDatabase db = DBHelper.open();
        db.beginTransaction();
        Long a = System.currentTimeMillis();
        Log.d(TAG, "updateAllPrice:" + customer.size());
        for (int i = 0; i < customer.size(); i++) {
            ClientPrice firstWarehouse = customer.get(i);
            ContentValues cv = new ContentValues();
            cv.put("id", firstWarehouse.getDealerId() + firstWarehouse.getSalerId()+firstWarehouse.getCustomerId()+"");
            cv.put("dealerId", firstWarehouse.getDealerId() + "");
            cv.put("priceTypeId", firstWarehouse.getPriceTypeId() + "");
            cv.put("orgId", firstWarehouse.getOrgId() + "");
            cv.put("salerId", firstWarehouse.getSalerId() + "");
            cv.put("customerId", firstWarehouse.getCustomerId() + "");
            cv.put("modifyTime", firstWarehouse.getModifyTime() + "");
            db.insertWithOnConflict("ClientPrice", null, cv, SQLiteDatabase.CONFLICT_REPLACE); //如果有记录，则更新，并返回1，否则返回0
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        Long b = System.currentTimeMillis();
        Log.i(TAG, String.valueOf(b - a));
        return 0;
    }

    public long getMaxModifyTime() {
        long param = 0;
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select max(modifyTime)from ClientPrice ", new String[]{});
        Log.d(TAG, "getMaxModifyTime:getCount" + cursor.getCount());
        if (cursor.moveToFirst()) {
            param = cursor.getLong(0);
        }
        cursor.close();
        Log.d(TAG, "getMaxModifyTime:param" + param);
        return param;
    }

    public void deleteAll() {
        SQLiteDatabase db = DBHelper.open();
        db.execSQL("DELETE FROM ClientPrice");
    }


    public String getPriceType(String dealerId, String orgId, String salerId,String customerId) {
        String priceType = null;
        SQLiteDatabase db = DBHelper.open();
        Log.d(TAG, "getPriceType:" + dealerId+"，"+orgId+"，"+salerId+"，"+customerId);
        //Cursor cursor = db.rawQuery("select * from ClientPrice where dealerId=? and orgId=? and salerId=?  and customerId=? ", new String[]{dealerId, orgId,salerId,customerId});
        Cursor cursor = db.rawQuery("select * from ClientPrice where dealerId=? and salerId=?  and customerId=? ", new String[]{dealerId, salerId,customerId});
        Log.d(TAG, "cursor:" + cursor.getCount());
        if (cursor.moveToFirst()) {
            priceType = cursor.getString(2);
        }
        cursor.close();
        Log.d(TAG, "getPriceType:" + priceType);
        return priceType;
    }

    public boolean hasData() {
        SQLiteDatabase db = DBHelper.open();
        Cursor cursor = db.rawQuery("select count(*)from ClientPrice", null);
        cursor.moveToFirst();
        Long count = cursor.getLong(0);
        if (count > 0) {
            cursor.close();
            Log.d(TAG, "hasPriceType true");
            return true;
        } else {
            Log.d(TAG, "hasPriceType false");
            cursor.close();
            return false;
        }
    }
}
